package com.feather.tenia.adapter.fetcher;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import com.feather.common.data.Column;
import com.feather.common.data.DataColumn;
import com.feather.common.data.DataItem;
import com.feather.common.db.DbFetcher;
import com.feather.tenia.adapter.data.String_DataColumn;

/**
 * @author flogyin
 */
public class NormalFetcher implements DbFetcher {
	public static final DbFetcher	INSTANCE	= new NormalFetcher();

	@Override
	public String getDbType() {
		return "char";
	}

	@Override
	public int getSqlType() {
		return Types.CHAR;
	}

	@Override
	public DataColumn fetch(ResultSet rs, Column column) throws SQLException {
		String data = rs.getString(column.getOrdinal());

		String_DataColumn dataColumn = new String_DataColumn(column);
		dataColumn.setString(data);
		return dataColumn;
	}

	@Override
	public DataItem fetch(CallableStatement statement, Column column) throws SQLException {
		String data = statement.getString(column.getOrdinal());

		String_DataColumn dataColumn = null;
		if (column.getName() != null && column.getName().startsWith("@")) {
			// for sqlserver SP param
			Column copyColumn = column.copy();
			copyColumn.setName(copyColumn.getName().substring(1));
			dataColumn = new String_DataColumn(copyColumn);
		} else {
			dataColumn = new String_DataColumn(column);
		}
		dataColumn.setString(data);
		return dataColumn;
	}
}
